package uno.rocc.framework.core.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.web.bind.annotation.*;
import uno.rocc.framework.core.annotation.RoccUnoAuthorize;
import uno.rocc.common.core.service.BaseService;
import uno.rocc.common.util.Result;

@Data
@EqualsAndHashCode(callSuper = true)
public class BaseCURDController<Q, T, R> extends

        BaseController {

    public final BaseService<Q, T, R> service;

    public BaseCURDController(BaseService<Q, T, R> service) {
        this.service = service;
    }

    @Operation(summary = "查询列表")
    @GetMapping("/list")
    @RoccUnoAuthorize(":list")
    public Result getList(Q queryParam) {
        Page<R> page = service.list(startPage(), queryParam);
        return Result.success(page);
    }

    @Operation(summary = "查询id详情")
    @GetMapping("/{id}")
    @RoccUnoAuthorize(":info")
    public Result getById(@PathVariable("id") Integer id) {
        T entity = service.getById(id);
        return Result.success(entity);
    }

    @Operation(summary = "新增对象")
    @PostMapping
    @RoccUnoAuthorize(":add")
    public Result save(@RequestBody T entity) {
        service.save(entity);
        return Result.success();
    }

    @Operation(summary = "修改对象")
    @PutMapping
    @RoccUnoAuthorize(":modify")
    public Result update(@RequestBody T entity) {
        service.updateById(entity);
        return Result.success();
    }

    @Operation(summary = "删除对象")
    @DeleteMapping("/{id}")
    @RoccUnoAuthorize(":remove")
    public Result delete(@PathVariable Integer id) {
        service.removeById(id);
        return Result.success();
    }


}
